<template>
  <BottomDrawer
    :visible="addVisible"
    title="异常提示"
    :width="880"
    @close="addVisible = false"
  >
    <div>
      <div>当前操作将影响以下患者就诊，请先为以下患者退号，才可进行操作！</div>
       <Table :columns="tableColumns"  style="margin-top: 10px;" :height="'500px'"
            :data-source="tableData" />
    </div>
  </BottomDrawer>
</template>

<script setup lang="ts">
import type { ShiftFrom, ShiftRole,  RangeForm,RangeList,RangeItem} from '@/types/registrationSettings/index.ts';
import type { TableColumn } from '@/types/table';
const loading = ref<boolean>(false)
import {getCancelRegisterList,cancelRegister} from '@/api/register/index'
import { useBaseDataStore } from '@/store/basedata';

const userEnums = useBaseDataStore();

const getPayStatusText = (payStatus: string) => {
  return userEnums
    .getEnumSelect('payStatusEnum')
    ?.find((target: any) => target.value?.toString() === payStatus?.toString())
    ?.text;
};

// 获取序列列表
defineExpose<{
  open: (row:any) => void;
}>({
  open: (row:any) => {
    addVisible.value = true;
    formData.value = row
    tableData.value = row.data.cancelList

    tableData.value.forEach((item)=>{
      item.payStatus = getPayStatusText(item.payStatus)
    })
    // getCancelRegisterList(row)
  },
});

const formData = ref<RangeForm>({
    "shiftId": 1, //班次ID；必须参数
    "doctorId": 1, //医生ID 员工请假场景需要给
    "rangeList": [
      
    ],
});


const tableData = ref<RangeItem[]>([]);
const pageSize = ref<number>(10)
const currentPage = ref<number>(1)
const total = ref<number>(0)
const tableColumns: TableColumn[] = [
  { prop: 'patientName', label: '患者' },
  { prop: 'patientPhone', label: '电话' },
  { prop: 'doctorName', label: '预约医生' },
  { prop: 'visitDate', label: '预约日期' },
  { prop: 'payStatus', label: '是否付款' },
  {
    prop: 'action',
    label: '操作',
    type: 'action',
    width: 170,
    componentProps: {
      fixed: 'right',
    },
    buttonGroup: [
      {
        label: '退号',  type: 'text', color: '#D90000', click: (row: any) => {

          cancelRegisterApi(row)
        }
      },
    ]
  }
]

const cancelRegisterApi = async (row: any) => {
  loading.value = true
  let res = await cancelRegister({ registerId: row.registerId })
  if (res.code == '200') {
    ElMessage({
      type: 'success',
      message: res.msg,
      duration: 2000,
    })
    closeDrawer()

  }else{
    //ElMessage.error(res.msg);
    return;
  }
  loading.value = false
}
const getCancelRegisterListApi =  async()=>{
  formData.value.pageNo = currentPage.value
  formData.value.pageSize = pageSize.value
  let res = await getCancelRegisterList(formData.value)
  if(res.code !== '200') {
    //ElMessage.error(res.msg);
    return;
  }
  tableData.value = res.data.list
}

const handlePageChange = (page: number) => {
  //console.log('当前页:', page);
  currentPage.value = page;
  getCancelRegisterListApi()
};
const handleSizeChange = (size: number) => {
  //console.log('每页条数:', size);
  pageSize.value = size;
  currentPage.value = 1; // 重置当前页为1
  getCancelRegisterListApi()

};

const addVisible = ref(false);






const emit = defineEmits(['closeDrawer']);
const closeDrawer = () => {
    addVisible.value = false
    emit('closeDrawer');
};
onMounted(() => {
});
</script>
<style lang="scss" scoped>
.tree-box {
  border: 2px solid #e1e1e1;
  height: 300px;
  width: 451px;
  margin-top: 10px;
  overflow-y: auto;
}
</style>
